导航菜单
首页 >  Crop an image  > Cropping an Image using OpenCV

Cropping an Image using OpenCV

Come, see for yourself, how Cropping an Image using OpenCV works. First, why do we need to crop? Cropping is done to remove all unwanted objects or areas from an image. Or even to highlight a particular feature of an image. 

There is no specific function for cropping using OpenCV, NumPy array slicing is what does the job. Every image that is read in, gets stored in a 2D array (for each color channel). Simply specify the height and width (in pixels) of the area to be cropped. And it’s done!

Cropping using OpenCVDiving an Image into Small PatchesInteresting ApplicationsSummary

The following code snippets show how to crop an image using both, Python and C++. Further in the post, you will get to learn about these in detail.

Python# Import packagesimport cv2import numpy as npimg = cv2.imread('test.jpg')print(img.shape) # Print image shapecv2.imshow("original", img)# Cropping an imagecropped_image = img[80:280, 150:330]# Display cropped imagecv2.imshow("cropped", cropped_image)# Save the cropped imagecv2.imwrite("Cropped Image.jpg", cropped_image)cv2.waitKey(0)cv2.destroyAllWindows()C++// Include Libraries#include#include// Namespace nullifies the use of cv::function();using namespace std;using namespace cv;int main(){// Read imageMat img = imread("test.jpg");cout

相关推荐: